﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CWB.Web.Models.Configuration;
using CWB.Web.MVC.BaseController;
using CWB.Web.MVC.ActionResults;
using log4net;
using System.Globalization;

namespace CWB.Web.MVC.ReturnResult
{
    public class RejectRequestResult
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(RejectRequestResult));

        public static void RejectRequest(ActionExecutingContext filterContext, RejectReason reason, SessionData sessionData)
        {
            string logoutUrl = getLogoutUrl(sessionData);

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = GetNoPermisionJsonResult(reason, logoutUrl);
            }
            else
            {
                filterContext.Result = GetNoPermisionRedirectResult(reason, logoutUrl);
            }
        }

        public static void RejectRequest(AuthorizationContext filterContext, RejectReason reason, SessionData sessionData)
        {
            string logoutUrl = getLogoutUrl(sessionData);

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = GetNoPermisionJsonResult(reason, logoutUrl);
            }
            else
            {
                filterContext.Result = GetNoPermisionRedirectResult(reason, logoutUrl);
            }
        }

        public static void RejectRequest(ExceptionContext filterContext, RejectReason reason, SessionData sessionData)
        {
            string logoutUrl = getLogoutUrl(sessionData);

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = GetNoPermisionJsonResult(reason, logoutUrl);
            }
            else
            {
                filterContext.Result = GetNoPermisionRedirectResult(reason, logoutUrl);
            }
        }

        private static ActionResult GetNoPermisionRedirectResult(RejectReason reason, string logoutUrl)
        {
            log.InfoFormat("Member Kicked Out. Reason: {0}. Redirect permanently(301) to {1}", reason.ToString(), logoutUrl);
            return new PermanentRedirectResult(logoutUrl, true);
        }

        private static ActionResult GetNoPermisionJsonResult(RejectReason reason, string logoutUrl)
        {
            object Reject = new RejectResult(reason, logoutUrl);

            return new JsonDataContractResult
            {
                Data = Reject,
                ContentType = "",
                ContentEncoding = Encoding.UTF8,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        
        private static string getLogoutUrl(SessionData sessionData)
        {
            string logouturl = string.Empty;
            string CultureName = string.Empty;
            string target = string.Empty;

            if ((sessionData != null) && (sessionData.trading != null) && (sessionData.trading.languageInfo != null))
            {
                CultureName = sessionData.trading.languageInfo.CultureName;
            }
            else
            {
                CultureName = CultureInfo.CurrentCulture.ToString().ToLower();
            }

            if ((sessionData != null) && (sessionData.trading != null) && !string.IsNullOrEmpty(sessionData.trading.logoutUrl))
            {
                target = sessionData.trading.logoutUrl;
            }
            else
            {
                target = "Login";
            }
            logouturl = string.Format("/{0}/{1}", CultureName, target);
            return logouturl;
        }
    }
}
